import request from "utils/request";
import axios from "axios";
import { getCookie, handleSearchValue, StringData,handleGetDealSearchValue } from "utils";
import { searchField } from './InitTemplate';

//定义接口地址
const URL = {
    "enableBill": `${GROBAL_HTTP_OCCBASE}customer/batch-enable`,
    "disableBill": `${GROBAL_HTTP_OCCBASE}customer/batch-disable`,
    "submitItem": `${GROBAL_HTTP_OCCBASE}customer/submitItem`,

    "GET_TOEXPORTEXCEL": `${GROBAL_HTTP_OCCBASE}base/retail-saleOrder-excel`,
    "NormalUrlDelete": `/occ-fee/fee/activity-scheme`, 
    "queryDetail": `/occ-fee/fee/activity-scheme/getId`, 
    "selectUrl": `/occ-base/cust-doc-defs/cust_doc_code/batch`,
    //列表
    "NormalUrl": `/occ-fee/fee/activity-scheme`,
    "SAVE": `/occ-fee/fee/activity-scheme/batch-save`,
    "aliaCustomer": `${GROBAL_HTTP_OCCBASE}customer/get-associated-customers`,
    "taxpayerNumberUrl": `${GROBAL_HTTP_OCCBASE}customer/get-rs-info`,
    "getDealModal": `${GROBAL_HTTP_OCCBASE}customer/create-supplier`,
    "createCustomer": `${GROBAL_HTTP_OCCBASE}customer/create-supplier`,
    "getTargetDealList": `/occ-fee/fee/activity-scheme`,
    "getPerson": `/occ-base/verify`,
    "getPic": `${GROBAL_HTTP_OCCBASE}pictures/batch-save`,
    "SubmitFile": `${GROBAL_HTTP_OCCBASE}customer-file`,
    "SubmitFileDelete": `${GROBAL_HTTP_OCCBASE}customer-file/delete`,
    "syncNCC":`/occ-fee/fee/activity-scheme/update-actualSaleAmount`,//更新销售数据
    "syncNCCList":`/occ-fee/fee/activity-scheme/update-actualSaleAmount-List`,//更新销售数据
    "updateInvalidDate":`/occ-fee/fee/activity-scheme/updateInvalidDate`,//更新失效日期
    "collectBill": `/occ-fee/fee/activity-scheme/collect-bill`,//收藏单据
    "cancelCollectBill": `/occ-fee/fee/activity-scheme/cancel-collect-bill`,//取消收藏单据
    "getShareList": `/occ-fee/fee/activity-scheme/get-share-info`,
    //"getShareList": `/occ-fee/fee/activity-scheme/getId`,
    "saveBillUrl": '/occ-fee/fee/activity-scheme/share-bill',
    "auth": `/wbalone/security/auth?funcCode=ActivityScheme`,
}

//传真
export const getAuth = () => {
    let url = `${URL.auth}`;
    return request(url, {
        method: "get",
    });
}

//更新销售数据
export const syncNCC = (data) => {
    let url = `${URL.syncNCC}`;
    return request(url, {
        method: "post",
        data:data
    });
}

//批量更新销售数据
export const syncNCCList = (data) => {
    let url = `${URL.syncNCCList}`;
    return request(url, {
        method: "post",
        data:data
    });
}

//更新销售数据
export const updateInvalidDate = (data) => {
    let url = `${URL.updateInvalidDate}`;
    return request(url, {
        method: "post",
        data:data
    });
}

//同步NCC
export const getShareList = (data) => {
    return request(URL.getShareList, {
        method: "get",
        param:{id:data}
    });
}

export const saveBill = (data) => {
    return request(URL.saveBillUrl, {
        method: "post",
        data: data
    });
}


//附件上传
export const submitFile = (data) => {
    return request(URL.NormalUrl, {
        method: "put",
        data: data,
    });
}

//附件上传
export const submitFileDelete = (data) => {
    return request(URL.SubmitFileDelete + "?ids=" + data.ids, {
        method: "post",
        data: data,
    });
}

export const getPerson = (param) => {
    let data = {
        userId: getCookie('_A_P_userId'), 
        cid: '' ,
        uuid:'',
    }
    return request(URL.getPerson, {
        method: "post",
        data:data
    });
}

export const getDealModal = (param) => {
    return request(param.url || URL.getTargetDealList, {
        method: "post",
        data: param.data
    });
}

export const getTargetDealList = (param) => {
    let value;
    if(!param.searchField) {
        value = param.param
    }else {
        value = handleGetDealSearchValue(param.param, param.searchField);
    }
    if(!value)  value = {};
    value.size = value.size || 10;
    value.search_AUTH_APPCODE = 'ActivityScheme';
    return request(param.url || URL.getTargetDealList, {
        method: "get",
        param: value
    });
}

export const createCustomer = (param) => {
    return request(URL.createCustomer, {
        method: "post",
        data: StringData(param),
        ContentType: "application/x-www-form-urlencoded;charset=utf-8"
    });
}

export const getAliaCustomer = (param) => {
    return request(`${URL.aliaCustomer}`, {
        method: "get",
        param: { id: param }
    });
}


export const getTaxpayerNumber = (param) => {
    return request(URL.taxpayerNumberUrl, {
        method: "get",
        param: param
    });
}

export const getSelectData = (param) => {
    return request(URL.selectUrl, {
        method: "get",
        param: { cust_doc_code_batch: param.toString() }
    });
}

export const enableBill = (param) => {
    return request(URL.enableBill, {
        method: "post",
        data: param
    });
}

export const disableBill = (param) => {
    return request(URL.disableBill, {
        method: "post",
        data: param
    });
}

export const submitItem = (param) => {
    return request(URL.submitItem, {
        method: "post",
        data: param
    });
}


/**
 * 获取列表
 * @param {*} params
 */
export const getList = (param) => {
    let value = handleSearchValue(param, searchField);
    return request(URL.NormalUrl, {
        method: "get",
        param: value
    });
}
{/**
param = {
    data:{},
    header:{
        url:"",
        method:"get"
    }
}
 */}
export const ButtonSetsService = (param) => {
    let { header, data } = param;
    if (header.ContentType == "application/x-www-form-urlencoded;charset=utf-8") {
        data = StringData(data);
    }
    return request(`/${header.url}` || URL.GET_LIST, {
        method: header.method || "post",
        data: data,
        ContentType: header.ContentType
    });
}



/**
 * 获取列表
 * @param {*} params
 */
// export const getList = (params) => {
//     let url =URL.GET_LIST+'?1=1';
//     for(let attr in params){
//         if((attr!='pageIndex')&&(attr!='pageSize')){
//             url+='&search_'+attr+'='+params[attr];
//         }else{
//             url+='&'+attr+'='+params[attr];
//         }
//     }
//     return request(url, {
//         method: "get",
//         data: params
//     });
// }

export const getChildList = (params) => {
    let url = URL.GET_ChildLIST + '?1=1';
    // for(let attr in params){
    //     if((attr!='pageIndex')&&(attr!='pageSize')){
    //         url+='&search_'+attr+'='+params[attr];
    //     }else{
    //         url+='&'+attr+'='+params[attr];
    //     }
    // }

    for (let attr in params.whereParams) {
        url += '&search_' + attr + '=' + params.whereParams[attr];

    }
    for (let attr1 in params.pageParams) {
        url += '&' + attr1 + '=' + params.pageParams[attr1];

    }

    return request(url, {
        method: "get",
        data: params
    });
}

/**
 * 获取下拉列表
 * @param {*} params
 */
export const OnResampling = (params) => {
    return request(URL.OnResampling, {
        method: "post",
        data: params
    });
}

export const VerificationCargo = (params) => {
    return request(URL.VerificationCargo, {
        method: "post",
        data: params
    });
}

export const submitBill = (params) => {
    return request(URL.SubmitBill, {
        method: "post",
        data: params
    });
}


/**
 * 删除table数据
 * @param {*} params
 */
export const deleteList = (params) => {
    return request(URL.DELETE, {
        method: "post",
        data: params
    });
}

export const saveList = (params) => {
    return request(URL.SAVE, {
        method: "post",
        data: params
    });
}
export const save = (params) => {
    return request(URL.NormalUrl, {
        method: "post",
        data: params
    });
}

export const updateToNCC = (params) => {
    return request(URL.NormalUrl, {
        method: "put",
        data: params
    });
}


export const del = (params) => {
    return request(URL.NormalUrlDelete, {
        method: "post",
        data: StringData(params),
        ContentType: "application/x-www-form-urlencoded;charset=utf-8"
    });
}

export const confirmCancelPumping = (params) => {
    return request(URL.ConfirmCancelPumping, {
        method: "post",
        data: params
    });
}

export const confirmVerify = (params) => {
    return request(URL.ConfirmVerify, {
        method: "post",
        data: params
    });
}

export const humanVerify = (params) => {
    return request(URL.HumanVerify, {
        method: "post",
        data: params
    });
}



/**
 * 通过search_id 查询列表详情
*/

export const getDetail = (params) => {
    let url = `${URL.queryDetail}`;
    return request(url, {
        method: "get",
        param:{id:params}
    });
}
// 打印

export const queryPrintTemplateAllocate = (params) => {
    return request(URL.GET_QUERYPRINTTEMPLATEALLOCATE, {
        method: "get",
        param: params
    });

}

export const printExcel = (params) => {

    let search = [];
    for (let key in params) {
        search.push(`${key}=${params[key]}`)
    }
    let exportUrl = `${URL.PRINTSERVER}?${search.join('&')}`;

    window.open(exportUrl);

}
/**
 * 导出
 */
export const exportExcel = (params) => {
    params.dataList.search_AUTH_APPCODE = "ActivityScheme";
    if(params.GET_TOEXPORTEXCEL.search("downloadExcelTemplate") == -1) {
        return request(params.GET_TOEXPORTEXCEL, {
            method: "post",
            data: StringData(params.dataList),
            ContentType: "application/x-www-form-urlencoded;charset=utf-8"
        });
    }else {
        exportData(params.GET_TOEXPORTEXCEL, params.dataList);
    }
}

export const collectBill = (params) => {
    return request(URL.collectBill, {
        method: "post",
        data: StringData(params),
        ContentType: "application/x-www-form-urlencoded;charset=utf-8"
    });
}

export const cancelCollectBill = (params) => {
    return request(URL.cancelCollectBill, {
        method: "post",
        data: StringData(params),
        ContentType: "application/x-www-form-urlencoded;charset=utf-8"
    });
}

const selfURL = window[window.webkitURL ? 'webkitURL' : 'URL'];
let exportData = (url, data) => {
    axios({
        method: 'post',
        url: url,
        data: data,
        responseType: 'blob'
    }).then((res) => {
        const content = res.data;
        const blob = new Blob([content]);
        const fileName = "导出数据.xls";

        let elink = document.createElement('a');
        if ('download' in elink) {
            elink.download = fileName;
            elink.style.display = 'none';
            elink.href = selfURL['createObjectURL'](blob);
            document.body.appendChild(elink);

            // 触发链接
            elink.click();
            selfURL.revokeObjectURL(elink.href);
            document.body.removeChild(elink)
        } else {
            navigator.msSaveBlob(blob, fileName);
        }
    })
}
